package com.ruoyi.dto;

import com.fasterxml.jackson.databind.ObjectMapper;

import java.text.SimpleDateFormat;
import java.util.Date;

public class MessageDTO {
    private Long fromUserId;
    private Long toUserId;
    private String content;
    private String type;
    private String timestamp;
    @Override
    public String toString() {
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            return objectMapper.writeValueAsString(this);
        } catch (Exception e) {
            // 处理异常，可根据实际情况进行调整
            e
                    .printStackTrace();
            return "{}";
        }
    }



    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(String timestamp) {
        this.timestamp = timestamp;
    }

    public Long getFromUserId() {
        return fromUserId;
    }

    public void setFromUserId(Long fromUserId) {
        this.fromUserId = fromUserId;
    }

    public Long getToUserId() {
        return toUserId;
    }

    public void setToUserId(Long toUserId) {
        this.toUserId = toUserId;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    // 新增方法：将时间戳转换为指定格式的时间字符串
    public Date getFormattedTime() {
        if (timestamp == null) {
            return new Date();
        }
        try {
            long timestampLong = Long.parseLong(timestamp);
            Date date = new Date(timestampLong);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 可以根据需要修改时间格式
            return date;
        } catch (NumberFormatException e) {
            return new Date();
        }
    }
}